using Microsoft.Phone.Controls.LocalizedResources;
using Microsoft.Phone.Shell;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
namespace Microsoft.Phone.Controls
{
	public class ListPickerPage : PhoneApplicationPage
	{
		private const string StateKey_Value = "ListPickerPage_State_Value";
		private PageOrientation lastOrientation;
		private static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register("isOIsOpenpen", typeof(bool), typeof(ListPickerPage), new PropertyMetadata(false, new PropertyChangedCallback(ListPickerPage.OnIsOpenChanged)));
		internal Grid MainGrid;
		internal TextBlock HeaderTitle;
		internal ListBox Picker;
		private bool _contentLoaded;
		public string HeaderText
		{
			get;
			set;
		}
		public IList Items
		{
			get;
			private set;
		}
		public SelectionMode SelectionMode
		{
			get;
			set;
		}
		public object SelectedItem
		{
			get;
			set;
		}
		public IList SelectedItems
		{
			get;
			private set;
		}
		public DataTemplate FullModeItemTemplate
		{
			get;
			set;
		}
		private bool IsOpen
		{
			get
			{
				return (bool)base.GetValue(ListPickerPage.IsOpenProperty);
			}
			set
			{
				base.SetValue(ListPickerPage.IsOpenProperty, value);
			}
		}
		private static void OnIsOpenChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			(o as ListPickerPage).OnIsOpenChanged();
		}
		private void OnIsOpenChanged()
		{
			this.UpdateVisualState(true);
		}
		public ListPickerPage()
		{
			this.InitializeComponent();
			this.Items = new List<object>();
			this.SelectedItems = new List<object>();
			base.add_Loaded(new RoutedEventHandler(this.OnLoaded));
			base.add_Unloaded(new RoutedEventHandler(this.OnUnloaded));
		}
		private void OnLoaded(object sender, RoutedEventArgs e)
		{
			base.add_OrientationChanged(new EventHandler<OrientationChangedEventArgs>(this.OnOrientationChanged));
			this.lastOrientation = base.get_Orientation();
			if (base.get_ApplicationBar() != null)
			{
				IEnumerator enumerator = base.get_ApplicationBar().get_Buttons().GetEnumerator();
				try
				{
					while (enumerator.MoveNext())
					{
						object current = enumerator.get_Current();
						IApplicationBarIconButton applicationBarIconButton = current as IApplicationBarIconButton;
						if (applicationBarIconButton != null)
						{
							if ("DONE" == applicationBarIconButton.get_Text())
							{
								applicationBarIconButton.set_Text(ControlResources.DateTimePickerDoneText);
								applicationBarIconButton.add_Click(new EventHandler(this.OnDoneButtonClick));
							}
							else
							{
								if ("CANCEL" == applicationBarIconButton.get_Text())
								{
									applicationBarIconButton.set_Text(ControlResources.DateTimePickerCancelText);
									applicationBarIconButton.add_Click(new EventHandler(this.OnCancelButtonClick));
								}
							}
						}
					}
				}
				finally
				{
					IDisposable disposable = enumerator as IDisposable;
					if (disposable != null)
					{
						disposable.Dispose();
					}
				}
			}
			this.SetupListItems(-90.0);
			PlaneProjection planeProjection = (PlaneProjection)this.HeaderTitle.get_Projection();
			if (planeProjection == null)
			{
				planeProjection = new PlaneProjection();
				this.HeaderTitle.set_Projection(planeProjection);
			}
			planeProjection.set_RotationX(-90.0);
			this.Picker.set_Opacity(1.0);
			base.get_Dispatcher().BeginInvoke(delegate
			{
				this.IsOpen = true;
			});
		}
		private void OnUnloaded(object sender, RoutedEventArgs e)
		{
			base.remove_OrientationChanged(new EventHandler<OrientationChangedEventArgs>(this.OnOrientationChanged));
		}
		private void SetupListItems(double degree)
		{
			for (int i = 0; i < this.Picker.get_Items().get_Count(); i++)
			{
				FrameworkElement frameworkElement = (FrameworkElement)this.Picker.get_ItemContainerGenerator().ContainerFromIndex(i);
				if (frameworkElement != null)
				{
					PlaneProjection planeProjection = (PlaneProjection)frameworkElement.get_Projection();
					if (planeProjection == null)
					{
						planeProjection = new PlaneProjection();
						frameworkElement.set_Projection(planeProjection);
					}
					planeProjection.set_RotationX(degree);
				}
			}
		}
		protected override void OnNavigatedTo(NavigationEventArgs e)
		{
			if (e == null)
			{
				throw new ArgumentNullException("e");
			}
			base.OnNavigatedTo(e);
			if (base.get_State().ContainsKey("ListPickerPage_State_Value"))
			{
				base.get_State().Remove("ListPickerPage_State_Value");
				if (base.get_NavigationService().get_CanGoBack())
				{
					base.get_NavigationService().GoBack();
					return;
				}
			}
			if (this.HeaderText != null)
			{
				this.HeaderTitle.set_Text(this.HeaderText.ToUpper(CultureInfo.get_CurrentCulture()));
			}
			this.Picker.set_DataContext(this.Items);
			this.Picker.set_SelectionMode(this.SelectionMode);
			if (this.FullModeItemTemplate != null)
			{
				this.Picker.set_ItemTemplate(this.FullModeItemTemplate);
			}
			if (this.SelectionMode == null)
			{
				base.get_ApplicationBar().set_IsVisible(false);
				this.Picker.set_SelectedItem(this.SelectedItem);
				return;
			}
			base.get_ApplicationBar().set_IsVisible(true);
			this.Picker.set_ItemContainerStyle((Style)base.get_Resources().get_Item("ListBoxItemCheckedStyle"));
			IEnumerator enumerator = this.Items.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					object current = enumerator.get_Current();
					if (this.SelectedItems != null && this.SelectedItems.Contains(current))
					{
						this.Picker.get_SelectedItems().Add(current);
					}
				}
			}
			finally
			{
				IDisposable disposable = enumerator as IDisposable;
				if (disposable != null)
				{
					disposable.Dispose();
				}
			}
		}
		private void OnDoneButtonClick(object sender, EventArgs e)
		{
			this.SelectedItem = this.Picker.get_SelectedItem();
			this.SelectedItems = this.Picker.get_SelectedItems();
			this.ClosePickerPage();
		}
		private void OnCancelButtonClick(object sender, EventArgs e)
		{
			this.SelectedItem = null;
			this.SelectedItems = null;
			this.ClosePickerPage();
		}
		protected override void OnBackKeyPress(CancelEventArgs e)
		{
			if (e == null)
			{
				throw new ArgumentNullException("e");
			}
			e.set_Cancel(true);
			this.SelectedItem = null;
			this.SelectedItems = null;
			this.ClosePickerPage();
		}
		private void ClosePickerPage()
		{
			this.IsOpen = false;
		}
		private void OnClosedStoryboardCompleted(object sender, EventArgs e)
		{
			base.get_NavigationService().GoBack();
		}
		protected override void OnNavigatedFrom(NavigationEventArgs e)
		{
			if (e == null)
			{
				throw new ArgumentNullException("e");
			}
			base.OnNavigatedFrom(e);
			if (e.get_Uri().IsExternalNavigation())
			{
				base.get_State().set_Item("ListPickerPage_State_Value", "ListPickerPage_State_Value");
			}
		}
		private void OnOrientationChanged(object sender, OrientationChangedEventArgs e)
		{
			PageOrientation orientation = e2.get_Orientation();
			RotateTransition rotateTransition = new RotateTransition();
			if (this.MainGrid != null)
			{
				PageOrientation pageOrientation = orientation;
				switch (pageOrientation)
				{
				case 1:
				case 5:
					this.HeaderTitle.set_Margin(new Thickness(20.0, 12.0, 12.0, 12.0));
					this.Picker.set_Margin(new Thickness(8.0, 12.0, 0.0, 0.0));
					rotateTransition.Mode = ((this.lastOrientation == 18) ? RotateTransitionMode.In90Counterclockwise : RotateTransitionMode.In90Clockwise);
					goto IL_1CC;
				case 2:
					break;
				case 3:
				case 4:
					goto IL_1CC;
				default:
					if (pageOrientation != 18)
					{
						if (pageOrientation != 34)
						{
							goto IL_1CC;
						}
						this.HeaderTitle.set_Margin(new Thickness(20.0, 0.0, 0.0, 0.0));
						this.Picker.set_Margin(new Thickness(8.0, 0.0, 0.0, 0.0));
						rotateTransition.Mode = ((this.lastOrientation == 5) ? RotateTransitionMode.In90Counterclockwise : RotateTransitionMode.In180Clockwise);
						goto IL_1CC;
					}
					break;
				}
				this.HeaderTitle.set_Margin(new Thickness(72.0, 0.0, 0.0, 0.0));
				this.Picker.set_Margin(new Thickness(60.0, 0.0, 0.0, 0.0));
				rotateTransition.Mode = ((this.lastOrientation == 34) ? RotateTransitionMode.In180Counterclockwise : RotateTransitionMode.In90Clockwise);
			}
			IL_1CC:
			PhoneApplicationPage element = (PhoneApplicationPage)((PhoneApplicationFrame)Application.get_Current().get_RootVisual()).get_Content();
			ITransition transition = rotateTransition.GetTransition(element);
			transition.Completed += delegate(object sender, EventArgs e)
			{
				transition.Stop();
			};
			transition.Begin();
			this.lastOrientation = orientation;
		}
		private void UpdateVisualState(bool useTransitions)
		{
			if (useTransitions)
			{
				if (!this.IsOpen)
				{
					this.SetupListItems(0.0);
				}
				Storyboard storyboard = new Storyboard();
				Storyboard storyboard2 = this.AnimationForElement(this.HeaderTitle, 0);
				storyboard.get_Children().Add(storyboard2);
				IList<WeakReference> itemsInViewPort = ItemsControlExtensions.GetItemsInViewPort(this.Picker);
				for (int i = 0; i < itemsInViewPort.get_Count(); i++)
				{
					FrameworkElement element = (FrameworkElement)itemsInViewPort.get_Item(i).get_Target();
					Storyboard storyboard3 = this.AnimationForElement(element, i + 1);
					storyboard.get_Children().Add(storyboard3);
				}
				base.get_Dispatcher().BeginInvoke(new Action(this.UpdateOutOfViewItems));
				if (!this.IsOpen)
				{
					storyboard.add_Completed(new EventHandler(this.OnClosedStoryboardCompleted));
				}
				storyboard.Begin();
				return;
			}
			if (!this.IsOpen)
			{
				this.OnClosedStoryboardCompleted(null, null);
			}
		}
		private Storyboard AnimationForElement(FrameworkElement element, int index)
		{
			double num = 30.0;
			double num2 = (double)(this.IsOpen ? 350 : 250);
			double num3 = (double)(this.IsOpen ? -45 : 0);
			double num4 = (double)(this.IsOpen ? 0 : 90);
			ExponentialEase exponentialEase = new ExponentialEase();
			exponentialEase.set_EasingMode(this.IsOpen ? 0 : 1);
			exponentialEase.set_Exponent(5.0);
			ExponentialEase easingFunction = exponentialEase;
			DoubleAnimation doubleAnimation = new DoubleAnimation();
			doubleAnimation.set_Duration(new Duration(TimeSpan.FromMilliseconds(num2)));
			doubleAnimation.set_From(new double?(num3));
			doubleAnimation.set_To(new double?(num4));
			doubleAnimation.set_EasingFunction(easingFunction);
			DoubleAnimation doubleAnimation2 = doubleAnimation;
			Storyboard.SetTarget(doubleAnimation2, element);
			Storyboard.SetTargetProperty(doubleAnimation2, new PropertyPath("(UIElement.Projection).(PlaneProjection.RotationX)", new object[0]));
			Storyboard storyboard = new Storyboard();
			storyboard.set_BeginTime(new TimeSpan?(TimeSpan.FromMilliseconds(num * (double)index)));
			storyboard.get_Children().Add(doubleAnimation2);
			return storyboard;
		}
		private void UpdateOutOfViewItems()
		{
			IList<WeakReference> itemsInViewPort = ItemsControlExtensions.GetItemsInViewPort(this.Picker);
			for (int i = 0; i < this.Picker.get_Items().get_Count(); i++)
			{
				FrameworkElement frameworkElement = (FrameworkElement)this.Picker.get_ItemContainerGenerator().ContainerFromIndex(i);
				if (frameworkElement != null)
				{
					bool flag = false;
					using (IEnumerator<WeakReference> enumerator = itemsInViewPort.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							WeakReference current = enumerator.get_Current();
							if (current.get_Target() == frameworkElement)
							{
								flag = true;
							}
						}
					}
					if (!flag)
					{
						frameworkElement.set_Opacity((double)(this.IsOpen ? 1 : 0));
						PlaneProjection planeProjection = frameworkElement.get_Projection() as PlaneProjection;
						if (planeProjection != null)
						{
							planeProjection.set_RotationX(0.0);
						}
					}
				}
			}
		}
		private void OnPickerTapped(object sender, GestureEventArgs e)
		{
			if (this.SelectionMode == null)
			{
				this.SelectedItem = this.Picker.get_SelectedItem();
				this.ClosePickerPage();
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/Microsoft.Phone.Controls.Toolkit;component/ListPicker/ListPickerPage.xaml", 2));
			this.MainGrid = (Grid)base.FindName("MainGrid");
			this.HeaderTitle = (TextBlock)base.FindName("HeaderTitle");
			this.Picker = (ListBox)base.FindName("Picker");
		}
	}
}
